<?php
error_reporting(E_ALL);

if(!isset($relPath)) $relPath = "./";
require_once "site_vars.php";
/*
    This is to used for identifying a phpbb user. It tries
    to match an existing cookie session so no username is
    required. Failing that, try login.

    http://wiki.phpbb.com/Authentication_plugins#login_method
*/

if(!defined('IN_PHPBB')) {
	define('IN_PHPBB', true);
}
$phpEx = substr(strrchr(__FILE__, '.'), 1);
        
// $phpbb_root_path = "/usr/share/phpbb3/www/";

// common.php instantiates $user and $auth
require_once $phpbb_root_path . "common.php";
require_once $phpbb_root_path . "includes/functions_posting.php";
// $user in phpbb extends session
$user->session_begin();
$auth->acl($user->data);


class DpPhpbb3
{
    private $_username;
    private $_user_row;

    public function __construct()
    {
        global $user;

        $this->_username = $user->data['username'];
    }

    public function __destruct() 
    {
    }
    
    public function IsRegistered()
    {
        global $user;
        return $user->data['is_registered'];
    }

    public function IsLoggedIn() 
    {
        return
            ! empty($this->_username)
            && strtolower($this->_username) != "anonymous";
    }

    /*
    return array from auth->login:
        array(
         'status' => status-code(int),
         'error_msg'=> status-message-id(text),
         'user_row'=> user-row(array),
        );
    */

    public function DoLogin($username, $password) 
    {
        global $auth;
        $autologin = false;
        $login = $auth->login($username, $password, $autologin);

        if(! $login || $login['status'] != LOGIN_SUCCESS)
        {
            $this->_user_row = null;
            $this->_username = null;
            return false;
        }
        $this->_user_row = $login['user_row'];
        $this->_username = $login['user_row']['username'];
        if(empty($this->_username) 
                || strtolower($this->_username) == 'anonymous') 
        {
            $this->_user_row = null;
            $this->_username = null;
            return false;
        }
        return true;
    }

    public function Uid() 
    {
        return empty($this->_user_row)
            || empty($this->_user_row['user_id'])
            ? null
            : $this->_user_row['user_id'];
    }

    public function bb_user()
    {
        return $this->_user_row;
    }

    public function Email() 
    {
        return empty($this->_user_row)
            ? null
            : $this->_user_row['user_email'];
    }

    public function Language() 
    {
        global $user;
        return empty($user->data['user_lang'])
            ? null
            : $user->data['user_lang'];
    }

    public function UserName() 
    {
        return $this->_username;
    }

    public function DoLogout() 
    {
        global $user;
        $user->session_kill();
    }

    public function InboxCount()
    {
        global $user;
        return $user->data['user_unread_privmsg'];
    }

    public function CreateThread($subj, $msg)
    {
        $dpsubject = utf8_normalize_nfc($subj);
        $dpmessage = utf8_normalize_nfc($msg);

        // variables to hold the parameters for submit_post
        $poll = $uid = $bitfield = $options = '';
                  
        generate_text_for_storage($dpsubject, $uid, $bitfield, 
                                $options, false, false, false);
        generate_text_for_storage($dpmessage, $uid, $bitfield, 
                                $options, true, true, true);

        $data = array(
            'forum_id'          => 23,
            'icon_id'           => false,
         
            'enable_bbcode'     => true,
            'enable_smilies'    => true,
            'enable_urls'       => true,
            'enable_sig'        => true,
     
            'message'           => $dpmessage,
            'message_md5'       => md5($dpmessage),
     
            'bbcode_bitfield'   => $bitfield,
            'bbcode_uid'        => $uid,
     
            'post_edit_locked'  => 0,
            'topic_title'       => $dpsubject,
            'notify_set'        => false,
            'notify'            => false,
            'post_time'         => 0,
            'forum_name'        => '',
            'enable_indexing'   => true,
     
            // 3.0.6
            'force_approved_state'  => true,
        );
        
        submit_post('post', $dpsubject, '', POST_NORMAL, 
                                        $poll, $data);
        return $data['post_id'];
    }

    public function CreateProjectThread($subj, $msg)
    {
        return $this->CreateThread($subj, $msg);
    }

    public function LatestTopicPostTime($topicid) {
        $sql = "SELECT 
                    topic_last_post_time AS post_time
                FROM phpbb_topics
                WHERE topic_id = $topicid";
        return $this->SqlOneValue($sql, "post_time");
    }
    private function SqlOneValue($sql, $fldname) {
        global $db;
        $result = $db->sql_query($sql);
        $row = $db->sql_fetchrow($result);
        $val = $row[$fldname];
        $db->sql_freeresult($result);
        return $val;
    }

    public function TopicReplyCount($topicid) {
        $sql = "SELECT topic_replies
                FROM phpbb_topics
                WHERE topic_id = $topicid";
        return $this->SqlOneValue($sql, "topic_replies");
    }
}
?>
